package game.block.blocks;

import game.block.command.ExplodeCommand;
import game.block.entity.MovableEntity;
import game.block.entity.TrapEntity;
import game.block.game.Board;
import game.block.tile.Tile;
import game.block.turn.Turn;

import java.util.ArrayList;

import com.badlogic.gdx.math.Vector2;

public class ExplosionBlock extends MovableEntity {

    private ArrayList<Tile> explodeTiles;

    public ExplosionBlock(Vector2 pos, Tile tile) {
        super(pos, tile, EXPLODE_TILE, TYPE.EXPLODE);
        explodeTiles = new ArrayList<Tile>();
    }

    @Override
    public boolean select(Turn currentTurn, Tile t, Board board) {
        if(super.select(currentTurn, t, board)){
            board.unhideAllTiles();
            return true;
        }
        else if (explodeTiles.contains(t)) {
            unhighlightAdjacent();
            currentTurn.addCommand(new ExplodeCommand(this, t, board));
            board.turnOver();
            board.setBlockSelected(null);
            board.unhideAllTiles();
            return true;
        }
        return false;
    }

    public void updateAvailableTiles(Board board) {
        super.updateAvailableTiles(board);
        int cPos = (int) (currentTile.getPosition().x / TILE_SIZE);
        int rPos = (int) (currentTile.getPosition().y / TILE_SIZE);
        int arrayPos = (board.getCols() - 1 - rPos) * board.getRows() + cPos;
        updateExplodeTiles(arrayPos, board);
    }
    
    @Override
    public void checkSurroundings(int arrayPos, Board board) {
        board.unhideAllTiles();
        super.checkSurroundings(arrayPos, board);
    }
    
    public void updateExplodeTiles(int arrayPos, Board board) {
        explodeTiles.clear();
        // add left tile
        if (arrayPos % board.getRows() != 0) {
            Tile tile = board.getTiles().get(arrayPos - 1);
            if (tile.containsWall()) {
                tile.changeColor(COLOR.EXPLODE);
                tile.getEntities().get(0).hideSprite();
                explodeTiles.add(tile);
            }
        }
        // add right tile
        if (arrayPos % board.getRows() != board.getRows() - 1) {
            Tile tile = board.getTiles().get(arrayPos + 1);
            if (tile.containsWall()) {
                tile.getEntities().get(0).hideSprite();
                tile.changeColor(COLOR.EXPLODE);
                explodeTiles.add(tile);
            }
        }
        // add top tile
        if (arrayPos - board.getRows() > 0) {
            Tile tile = board.getTiles().get(arrayPos - board.getRows());
            if (tile.containsWall()) {
                tile.getEntities().get(0).hideSprite();
                tile.changeColor(COLOR.EXPLODE);
                explodeTiles.add(tile);
            }
        }
        // add bottom tile
        if (arrayPos + board.getRows() < board.getTiles().size()) {
            Tile tile = board.getTiles().get(arrayPos + board.getRows());
            if (tile.containsWall()) {
                tile.getEntities().get(0).hideSprite();
                tile.changeColor(COLOR.EXPLODE);
                explodeTiles.add(tile);
            }
        }
    }
    
    @Override
    public void unhighlightAdjacent() {
        super.unhighlightAdjacent();
        for (Tile tile : explodeTiles) {
            tile.changeColor(COLOR.BLANK);
        }
    }

    @Override
    public boolean counterEffect(TrapEntity te) {
        return false;
    }

	@Override
	public void setInfo() {
		info="Name:  Explosion Block\n\nFunction:  Destroy a wall by moving into it";
	}

}
